package com.cxria.sms.service.impl;

import com.cxria.sms.module.User;
import com.cxria.sms.persistence.PersistenceException;
import com.cxria.sms.persistence.PersistenceService;
import com.cxria.sms.service.ServiceException;
import com.cxria.sms.service.UserService;

import java.util.List;
import java.util.Optional;

public class UserServiceImpl implements UserService {

    private PersistenceService persistenceService = PersistenceServiceLoader.persistenceService;

    @Override
    public List<User> list() throws ServiceException {
        try {
            return persistenceService.list(User.class);
        } catch (PersistenceException e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    @Override
    public Optional<User> getUser(String id) throws ServiceException {
        try {
            return persistenceService.get(User.class, id);
        } catch (PersistenceException e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    @Override
    public void addUser(User user) throws ServiceException {
        try {
            persistenceService.save(user);
        } catch (PersistenceException e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    @Override
    public void updateUser(User user) throws ServiceException {
        try {
            persistenceService.save(user);
        } catch (PersistenceException e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

    @Override
    public void deleteUser(String id) throws ServiceException {
        try {
            persistenceService.delete(User.class, id);
        } catch (PersistenceException e) {
            e.printStackTrace();
            throw new ServiceException(e);
        }
    }

}
